package com.keqing;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;


public class GroupMemListener extends AnalysisEventListener<GroupMem> {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(GroupMemListener.class);

	private static final int BATCH_COUNT = 5;
	
	List<GroupMem> list = new ArrayList<>();

	private DemoDAO demoDAO;

	public GroupMemListener(DemoDAO demoDAO) {
		this.demoDAO = demoDAO;
	}


	@Override
	public void invoke(GroupMem data, AnalysisContext context) {
		LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
		list.add(data);
		if (list.size() >= BATCH_COUNT) {
			saveData();
			list.clear();
		}
	}

	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {
		saveData();
		LOGGER.info("所有数据解析完成！");
	}

	/**
	 * 加上存储数据库
	 */
	private void saveData() {
		LOGGER.info("{}条数据，开始存储数据库！", list.size());
		//demoDAO.save(list);
		LOGGER.info("存储数据库成功！");
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}